package cn.itsource.zcn.searchmastermessage.service.impl;

import cn.itsource.zcn.basic.domain.Point;
import cn.itsource.zcn.org.domain.Employee;
import cn.itsource.zcn.org.domain.Shop;
import cn.itsource.zcn.org.mapper.EmployeeMapper;
import cn.itsource.zcn.org.mapper.ShopMapper;
import cn.itsource.zcn.org.query.ShopQuery;
import cn.itsource.zcn.searchmastermessage.domain.SearchMasterMessage;
import cn.itsource.zcn.searchmastermessage.mapper.SearchMasterMessageMapper;
import cn.itsource.zcn.searchmastermessage.query.SearchMasterMessageQuery;
import cn.itsource.zcn.searchmastermessage.service.ISearchMasterMessageService;
import cn.itsource.zcn.user.domain.Logininfo;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.DistanceUtil;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (SearchMasterMessage)表服务实现类
 *
 * @author makejava
 * @since 2021-08-22 16:58:48
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class SearchMasterMessageServiceImpl implements ISearchMasterMessageService {
    @Autowired
    private SearchMasterMessageMapper tSearchMasterMessageMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 新增数据
     *
     * @param tSearchMasterMessage 实例对象
     */
    @Override
    @Transactional
    public void save(SearchMasterMessage tSearchMasterMessage) {
        tSearchMasterMessageMapper.save(tSearchMasterMessage);
    }

    /**
     * 修改数据
     *
     * @param tSearchMasterMessage 实例对象
     */
    @Override
    @Transactional
    public void update(SearchMasterMessage tSearchMasterMessage) {
        tSearchMasterMessageMapper.update(tSearchMasterMessage);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    @Transactional
    public void delete(Long id) {
        tSearchMasterMessageMapper.delete(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SearchMasterMessage queryById(Long id) {
        return tSearchMasterMessageMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<SearchMasterMessage> queryAll() {
        return tSearchMasterMessageMapper.queryAll();
    }

    /**
     * 寻主信息发布
     * @param searchMasterMessage
     * @return
     */
    @Override
    public AjaxResult publish(SearchMasterMessage searchMasterMessage, HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            // 寻主信息获取(拿到当前发布信息的用户)
            Logininfo logininfo = (Logininfo) redisTemplate.opsForValue().get(token);
            Long uid = logininfo.getUid();
        /*
                    name: '',
                    price: 0,
                    age: 0,
                    gender: 1,
                    coat_color: '',
                    resources: '',
                    pet_type: null,
                    address: '',
                    title: '',
         */
            // 设置成代处理状态
            searchMasterMessage.setState(0);
            // 设置当前发布信息的用户
            searchMasterMessage.setUser_id(uid);
            // 拿到用户填写的地址的坐标
            Point point = DistanceUtil.getPoint(searchMasterMessage.getAddress());
            // 拿到全部门店
            List<Shop> shops = shopMapper.queryAll();
            // 工具类使用获取最近的门店
            Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
            searchMasterMessage.setShop_id(nearestShop.getId());
            //调用方法保存寻主信息
            tSearchMasterMessageMapper.save(searchMasterMessage);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @Override
    public void deleteBatch(Long[] ids) {

    }

    @Override
    public QueryResult queryPage(SearchMasterMessageQuery searchMasterMessageQuery, HttpServletRequest request) {
        String token = request.getHeader("token");
        // 寻主信息获取(拿到当前发布信息的用户)
        Logininfo logininfo = (Logininfo) redisTemplate.opsForValue().get(token);
        Employee employee = employeeMapper.queryById(logininfo.getUid());
        Shop shop = shopMapper.queryByAdminid(employee.getId());
        // 把当前登录用户的id存到查询条件中
        searchMasterMessageQuery.setSid(shop.getId());

        Integer count = tSearchMasterMessageMapper.count(searchMasterMessageQuery);
        List<SearchMasterMessage> page = tSearchMasterMessageMapper.page(searchMasterMessageQuery);
        return new QueryResult(page,count);
    }


}
